package com.astronomia.wheels.pojo;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 三级树拼数据
 **/

@Data
public class TreePojo {

    private Integer id;

    private Integer pId;

    private Integer tId;

    private String name;

    private List<TreePojo> treePojoList;

    public TreePojo() {
    }

    public TreePojo(Integer id, Integer pId, Integer tId, String name) {
        this.id = id;
        this.pId = pId;
        this.tId = tId;
        this.name = name;
    }

    public static List<TreePojo> getList() {
        TreePojo treePojo3 = new TreePojo(1, null, null, "三级t1");
        TreePojo treePojo1 = new TreePojo(2, 1, 1, "三级p1");
        TreePojo treePojo2 = new TreePojo(3, 2, 1, "三级s1");

        TreePojo treePojo4 = new TreePojo(4, null, null, "三级t2");
        TreePojo treePojo5 = new TreePojo(5, 4, null, "三级p2");
        TreePojo treePojo6 = new TreePojo(6, 5, null, "三级s2");

        TreePojo treePojo7 = new TreePojo(7, null, null, "二级p3");
        TreePojo treePojo8 = new TreePojo(8, 7, null, "二级s3");

        TreePojo treePojo9 = new TreePojo(9, null, null, "二级p4");
        TreePojo treePojo10 = new TreePojo(10, 9, null, "二级s4");

        TreePojo treePojo11 = new TreePojo(11, null, null, "一级s4");
        return Arrays.asList(treePojo1, treePojo2, treePojo3, treePojo4, treePojo5, treePojo6, treePojo7, treePojo8, treePojo9, treePojo10, treePojo11);
    }

    public static void main(String[] args) {
        List<TreePojo> list = getList();
        List<TreePojo> collect = list.stream()
                .filter(l1 -> l1.pId == null && l1.tId == null)
                .peek(p1 -> p1.setTreePojoList(
                        list.stream()
                                .filter(l2 -> p1.getId().equals(l2.getPId()))
                                .peek(p2 -> p2.setTreePojoList(
                                        list.stream()
                                                .filter(l3 -> p2.getId().equals(l3.getPId()))
                                                .collect(Collectors.toList())))
                                .collect(Collectors.toList())))
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(collect));
    }

}
